package de.baumann.browser.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 关于sd卡的一些操作：sd卡是否有效，创建文件等
 *
 * @author wufengjun
 */
public class FileUtil {
    public static final int FILE_TYPE_BASE = 0;
    public static final int FILE_TYPE_IMAGES = 4;

    private static final String BASEPATH = "/odin";
    private static final String IMAGE = "/image";

    /**
     * 创建文件
     *
     * @param name
     * @param type
     * @return
     */
    public static File creatFile(String name, int type) {
        return new File(getFilePath(name, type));
    }

    /**
     * 获取文件路径
     *
     * @param name
     * @param type
     * @return
     */
    public static String getFilePath(String name, int type) {
        return getDir(type) + "/" + name;
    }

    public static String getDir(int type) {
        new File(getESD() + BASEPATH).mkdir();
        new File(getESD() + BASEPATH + IMAGE).mkdir();

        switch (type) {
            case FILE_TYPE_BASE:
                return getESD() + BASEPATH;
            case FILE_TYPE_IMAGES:
                return getESD()
                        + BASEPATH + IMAGE;
            default:
                return getESD() + BASEPATH;
        }
    }

    public static String getRealFilePath(final Context context, final Uri uri) {
        if (null == uri) {
            return null;
        }
        final String scheme = uri.getScheme();
        String data = null;
        if (scheme == null) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
            data = uri.getPath();
        } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
            Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.ImageColumns.DATA}, null, null, null);
            if (null != cursor) {
                if (cursor.moveToFirst()) {
                    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                    if (index > -1) {
                        data = cursor.getString(index);
                    }
                }
                cursor.close();
            }
        }
        return data;
    }

    /**
     * 获取sd卡文件路径
     *
     * @return
     */
    public static File getESD() {
        return Environment.getExternalStorageDirectory();
    }

    public static String getFileName(String filePath) {
        File file = new File(filePath);
        return file.getName();
    }

    /**
     * 通知系统扫描sd卡中文件 ，这货只能扫面文件 不能 扫面 文件夹
     *
     * @param context
     * @param file
     */
    public static void scanFile(Context context, File file) {
        Uri uri = Uri.fromFile(file);
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
    }


    public static Bitmap getImageFromAssetsFile(Context context, String fileName) {
        Bitmap image = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len = 0;
        AssetManager am = context.getResources().getAssets();
        try {
            InputStream is = am.open(fileName);
            while ((len = is.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            byte[] result = outputStream.toByteArray();
            image = BitmapFactory.decodeByteArray(result, 0, result.length);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return image;
    }

}
